var util = require('../../libs/util.js');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    tab:4,
    pageSize: 10,
    pageNumber: 0,
    count: 0,
    page: 1,
    firstPage: true,
    prevPage: true,
    nextPage: true,
    lastPage: true,
    progress: 0,
    totalBytesSent: 0,
    totalBytesExpectedToSend: 0,
    formType : 'add'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.setData({
      customer:e.id
    })
    this.loadData();
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  search: function () {
    this.loadData();
  },
  loadData: function () {
    var that = this;
    util.getData("/atapi/customer/customer/project", {
      'customer': that.data.customer,
      'searchVal': that.data.searchVal,
      'page': that.data.page,
      'pageSize': that.data.pageSize
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        var count = res.data.data[1][0].COUNT;
        var pageNumber = Math.ceil(count / that.data.pageSize);
        that.setData({
          dataList: res.data.data[0],
          count: count,
          pageNumber: pageNumber,
          page: that.data.page == 0 ? 1 : that.data.page
        });
        that.setPageStatus();
      } else {
        that.setData({
          page: 0,
          dataList: [],
          user_id:app.globalData.user_id
        });
      }
    });
  },
  //翻页按钮状态
  setPageStatus: function () {
    var that = this;
    var page = that.data.page, pageNumber = that.data.pageNumber;
    if (pageNumber == 1) {
      that.setData({
        firstPage: true,
        prevPage: true,
        nextPage: true,
        lastPage: true
      });
    } else if (pageNumber > 1) {
      if (page == 1) {
        that.setData({
          firstPage: true,
          prevPage: true,
          nextPage: false,
          lastPage: false
        });
      } else if (page == pageNumber) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: true,
          lastPage: true
        });
      } else if (page < pageNumber && page > 1) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: false,
          lastPage: false
        });
      }
    }
  },
  //分页按钮操作
  pageChange: function (e) {
    var type = e.currentTarget.dataset.page;
    if (type == 'first') {
      this.setData({
        page: 1
      })
      this.loadData();
    }
    if (type == 'last') {
      this.setData({
        page: this.data.pageNumber
      })
      this.loadData();
    }
    if (type == 'prev') {
      this.setData({
        page: this.data.page - 1
      })
      this.loadData();
    }
    if (type == 'next') {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    }
  },
  showModal(e) {
    var that = this;
    this.setData({
      modalName: e.currentTarget.dataset.target,
      modalTitle: e.currentTarget.dataset.title,
      modalType:e.currentTarget.dataset.type,
      formType:e.currentTarget.dataset.type
    })
    if(e.currentTarget.dataset.type == 'detail'){
       var id = e.currentTarget.id;
       var dataList = that.data.dataList;
       for(var i=0;i<dataList.length;i++){
         if(dataList[i].ID == id){
           var currentData = [];
           currentData.push({name:'名称',value:dataList[i].name});
            that.setData({
              _currentData:dataList[i],
               currentData:currentData
            })
         }
       }
    }
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  //提交表单
  saveFormData: function () {
    var that = this;

    var formData = {};

    formData.name = that.data.name||'';
    console.log(formData)
    if (formData.name == '') {
      wx.showToast({
        title: "请填写名称",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    wx.showLoading({
      title: '提交中...',
      mask: true
    })
    formData.CO_ID = app.globalData.CO_ID;
    formData.ADD_USER = app.globalData.user_id;
    formData.customer= that.data.customer;
    if (that.data.formType == 'add'){
      util.postData("/atapi/customer/customer/newProject", formData, function (res, error) {
        console.log(res); console.log(error)
        wx.hideLoading()
        if (res.data.success) {
          wx.showToast({
            title: "提交成功",
            icon: "success",
            duration: 3000,
            success: function () {
              setTimeout(function () {
                that.hideModal();
                that.loadData();
              }, 3000);
            }
          });
        } else {
          wx.showToast({
            title: "提交失败",
            image: '/images/wrong.png',
            duration: 3000
          });
        }
      });
    }else{
      formData.id = that.data.ID;
      util.putData("/atapi/customer/customer/editProject", formData, function (res, error) {
        console.log(res); console.log(error)
        wx.hideLoading()
        if (res.data.success) {
          wx.showToast({
            title: "编辑成功",
            icon: "success",
            duration: 3000,
            success: function () {
              setTimeout(function () {
                that.hideModal();
                that.loadData();
              }, 3000);
            }
          });
        } else {
          wx.showToast({
            title: "编辑失败",
            image: '/images/wrong.png',
            duration: 3000
          });
        }
      });
    }
    
  },
  editForm:function(){
    var that = this;
    that.setData({
      formType:'edit',
      modalName: 'detailModal',
      modalTitle: '修改客户项目',
      modalType:'edit'
    })
    that.setData(that.data._currentData)
    //if()
  },
  deleteForm:function(){
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '确定要删除吗？',
      success (res) {
        if (res.confirm) {
          util.postData("/atapi/customer/customer/deleteProject", {
             id:that.data._currentData.ID
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading()
            if (res.data.success) {
              wx.showToast({
                title: "删除成功",
                icon: "success",
                duration: 3000,
                success: function () {
                  setTimeout(function () {
                    that.hideModal();
                    that.loadData();
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: "删除失败",
                image: '/images/wrong.png',
                duration: 3000
              });
            }
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})